package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.lsn.SendMsgCenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatChatAddSystipActivity extends BaseActivity {
    @BindView(R2.id.rb_dazhaohu)
    RadioButton    rbDazhaohu;
    @BindView(R2.id.rb_tianjiahaoyou)
    RadioButton    rbTianjiahaoyou;
    @BindView(R2.id.rb_chehui)
    RadioButton    rbChehui;
    @BindView(R2.id.rb_moshengren)
    RadioButton    rbMoshengren;
    @BindView(R2.id.rb_weizhigongxiang)
    RadioButton    rbWeizhigongxiang;
    @BindView(R2.id.rg)
    RadioGroup     rg;
    @BindView(R2.id.tv_sender_name)
    TextView       tvSenderName;
    @BindView(R2.id.iv_avatar)
    ImageView      ivAvatar;
    @BindView(R2.id.iv_arrow)
    ImageView      ivArrow;
    @BindView(R2.id.btn_pick_sender)
    RelativeLayout btnPickSender;
    @BindView(R2.id.rv)
    RecyclerView   rv;
    @BindView(R2.id.tv_name)
    TextView       tvName;
    @BindView(R2.id.tv_preview)
    TextView       tvPreview;
    @BindView(R2.id.btn_save)
    Button         btnSave;

    private WechatContactBean mWechatContactBean, sender;
    private GroupTalkBean           mGroupTalkBean;
    private boolean                 isRoomChat;
    private ContactAdapter          adapter;
    private List<WechatContactBean> list;
    private String                  objUser = "我";

    public WechatChatAddSystipActivity() {
        super(R.layout.wechat_activity_chat_send_systip);
    }

    public static void start(Activity act, WechatContactBean contactBean) {
        Intent it = new Intent(act, WechatChatAddSystipActivity.class);
        it.putExtra("isRoomChat", false);
        it.putExtra("WechatContactBean", contactBean);
        act.startActivity(it);
    }

    public static void start(Context act, GroupTalkBean groupTalkBean) {
        Intent it = new Intent(act, WechatChatAddSystipActivity.class);
        it.putExtra("isRoomChat", true);
        it.putExtra("GroupTalkBean", groupTalkBean);
        act.startActivity(it);
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        //添加数据（me）
        WechatContactBean me = WechatContactBean.instanceNewContactMe(this);
        onSenderChange(me);
        list.add(me);
        //添加数据（other）
        isRoomChat = getIntent().getBooleanExtra("isRoomChat", false);
        if (!isRoomChat) {
            mWechatContactBean = (WechatContactBean) getIntent().getSerializableExtra("WechatContactBean");
            list.add(mWechatContactBean);
        } else {//群聊
            mGroupTalkBean = (GroupTalkBean) getIntent().getSerializableExtra("GroupTalkBean");
            list.addAll(mGroupTalkBean.getGroupMembersList());
        }
        //发送者选择adapter
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        adapter = new ContactAdapter(R.layout.wechat_select_sender_contact_item, list);
        rv.setAdapter(adapter);
    }

    @OnClick({R2.id.rb_dazhaohu, R2.id.rb_tianjiahaoyou, R2.id.rb_chehui,
            R2.id.rb_moshengren, R2.id.rb_weizhigongxiang, R2.id.btn_pick_sender, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_pick_sender) {
            if (rv.getVisibility() == View.VISIBLE) {
                rv.setVisibility(View.GONE);
            } else {
                rv.setVisibility(View.VISIBLE);
            }
        } else if (i == R.id.btn_save) {
            String content = tvPreview.getText().toString();
            SendMsgCenter.sendSystip(new SendMsgCenter.SendMsgListener() {
                @Override
                public void onSendSuc() {
                    finish();
                }

                @Override
                public void onSendError(String errorMsg) {

                }
            }, isRoomChat, isRoomChat ? mGroupTalkBean.getId() : mWechatContactBean.getId(), content);
        } else if (i == R.id.rb_dazhaohu) {
            setTipTemple("以上是打招呼的内容");
            setSenderSelectViewVisible(false);
        } else if (i == R.id.rb_tianjiahaoyou) {
            setTipTemple("你已添加了【对象】，现在可以开始聊天了。");
            setSenderSelectViewVisible(true);
        } else if (i == R.id.rb_chehui) {
            setTipTemple("【对象】撤回了一条消息");
            setSenderSelectViewVisible(true);
        } else if (i == R.id.rb_moshengren) {
            setTipTemple("如果陌生人主动添加你为朋友，请谨慎核实对方身份");
            setSenderSelectViewVisible(false);
        } else if (i == R.id.rb_weizhigongxiang) {
            setTipTemple("位置共享已经结束");
            setSenderSelectViewVisible(false);
        }
    }

    private void setSenderSelectViewVisible(boolean isShow) {
        btnPickSender.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    private String tipTemple = "以上是打招呼的内容";

    private void setTipTemple(String tip) {
        tipTemple = tip;
        updatePreviewShow();
    }

    private void updatePreviewShow() {
        String text = tipTemple.replace("【对象】", objUser);
        tvPreview.setText(text);
    }

    public class ContactAdapter extends BaseQuickAdapter<WechatContactBean, BaseViewHolder> {
        public ContactAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final WechatContactBean vo) {
            helper.setText(R.id.name, vo.getName());
            WechatContactBean.setupAvatarIntoImageView(vo,
                    (ImageView) helper.getView(R.id.iv_avatar), 12);
            helper.setChecked(R.id.rb, vo.isSelected());
            helper.getView(R.id.vg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (WechatContactBean item : ContactAdapter.this.getData()) {
                        item.setSelected(false);
                    }
                    notifyDataSetChanged();
                    onSenderChange(vo);
                }
            });
        }
    }

    private void onSenderChange(WechatContactBean vo) {
        vo.setSelected(true);
        sender = vo;
        rv.setVisibility(View.GONE);
        WechatContactBean.setupAvatarIntoImageView(vo, ivAvatar, 12);
        tvSenderName.setText(vo.getName());
        objUser = vo.getId() == -1 ? "我" : vo.getName();
        updatePreviewShow();
    }
}
